package com.example.myapplication;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtils {
    private OkHttpClient client = new OkHttpClient();
    private static String host = "http://";

    private void post(String api, String[] keys, Object... values) {
        FormBody.Builder fromBuilder = new FormBody.Builder();
        for (int i = 0; i < keys.length; i++) {
            fromBuilder.add(keys[i], (String) values[i]);
        }
        RequestBody requestBody = fromBuilder.build();
        Request.Builder requestBuilder = new Request.Builder().url(host +api).post(requestBody);
        execute(requestBuilder);
    }

    private void get(String api) {
        Request.Builder builder = new Request.Builder().url(host + api);
        execute(builder);
    }

    private void execute(Request.Builder builder) {
        Call call = client.newCall(builder.build());
        call.enqueue(callback);
    }

    private Callback callback = new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String json = new String(response.body().bytes(), "utf-8");
        }
    };
}
